package comment

import (
	"enjoy-shop/api/base"
	"enjoy-shop/formdata"
	"enjoy-shop/server/comment"
	"errors"
	"github.com/gin-gonic/gin"
)

func Check(data formdata.AddComment) error {
	if data.UserId == 0 {
		return errors.New("请先登录用户，登录之后可发布评论")
	}
	switch data.ObjectType {
	case 1:
		if data.Rating == 0 {
			return errors.New("评分不可为0")
		}
	case 2:
		if data.Text == "" {
			return errors.New("评论内容不能为空")
		}
	default:
		return errors.New("未知类型")
	}
	return nil
}

func AddComment(c *gin.Context) {
	var data formdata.AddComment
	err := c.ShouldBindJSON(&data)
	if err != nil {
		base.RespData(c, "", err)
		return
	}
	err = Check(data)
	if err != nil {
		base.RespData(c, "", err)
		return
	}

	addComment, err := comment.AddComment(&data)
	base.RespData(c, addComment, err)
}
